﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Using the DbContextManager</title>
  <keywords>
    <keyword translate="true">DbContext</keyword>
    <keyword translate="true">EF</keyword>
    <keyword translate="true">Entity Framework</keyword>
    <keyword translate="true">EntityFramework</keyword>
    <keyword translate="true">ObjectContext</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Using the DbContextManager</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">The </text><text styleclass="Normal" style="font-style:italic;" translate="true">DbContextManager</text><text styleclass="Normal" translate="true"> class allows the sharing of </text><text styleclass="Normal" style="font-style:italic;" translate="true">DbContext</text><text styleclass="Normal" translate="true"> (with underlying </text><text styleclass="Normal" style="font-style:italic;" translate="true">ObjectContext</text><text styleclass="Normal" translate="true">) classes in Entity Framework 5. The good thing about this is that the same context can be used in the same scope without having to recreate the same type of the same context over and over again.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">A very good example of this scoping is an ASP.NET (MVC) application where a context is normally shared throughout the whole thread that handles a request. A big advantage of reusing the same instance of a context is that already fetched entities don&apos;t need to be refetched over and over again.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Obtaining a DbContext</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Obtaining a </text><text styleclass="Normal" style="font-style:italic;" translate="true">DbContext</text><text styleclass="Normal" translate="true"> is very simple by using the </text><text styleclass="Normal" style="font-style:italic;" translate="true">DbContextManager</text><text styleclass="Normal" translate="true">.</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; text-decoration:none; color:#000000;" translate="true">using</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">(var</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">dbContextManager</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#ffffff;" translate="true"> </text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">DbContextManager&lt;MyEntities&gt;.GetManager())</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    var dbContext = dbContextManager.DbContext;</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">    // TODO: handle logic with dbContext here</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">}</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">It is very important to wrap the DbContextManager in a using state because it must be disposed</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Scoping is all done automatically because when a </text><text styleclass="Normal" style="font-style:italic;" translate="true">DbContextManager</text><text styleclass="Normal" translate="true"> is instantiated, a reference counter is increased. Everytime an instance of the </text><text styleclass="Normal" style="font-style:italic;" translate="true">DbContextManager</text><text styleclass="Normal" translate="true"> is disposed, the reference counter is decreased. When the reference count reaches zero (0), it will dispose the </text><text styleclass="Normal" style="font-style:italic;" translate="true">DbContext</text><text styleclass="Normal" translate="true"> that it manages.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Sharing a single DbContext per ASP.NET request</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">When a request is started, a context can be created by calling this code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">DbContextManagerHelper.CreateDbContextForHttpContext&lt;MyEntities&gt;();</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">When a request is ended, the context can be disposed by using this code:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; text-decoration:none; color:#000000;" translate="true">DbContextManagerHelper.DisposeDbContextForHttpContext&lt;MyEntities&gt;();</text></para>
    <para styleclass="Normal"></para>
  </body>
</topic>
